import { createI18n } from 'vue-i18n'
import { getLanguage } from '@/util/cookies'
import zh from './zh'
import en from './en'
import tc from './tc'

import viewzh from 'view-ui-plus/dist/locale/zh-CN';
import viewen from 'view-ui-plus/dist/locale/en-US';
import viewtw from 'view-ui-plus/dist/locale/zh-TW';

const messages = {
    'en-US': {
        ...en, ...viewen
    },
    'zh-CN': {
        ...zh, ...viewzh
    },
    'zh-TW': {
        ...tc, ...viewtw
    }
}

export const getLocale = () => {
    //读取cookie存入的当前语言
    const cookieLanguage = getLanguage()
    //如果有返回当前语言
    if (cookieLanguage) {
        return cookieLanguage
    }
    //如果没有，获取系统语言
    const language = navigator.language.toLowerCase()
    //获取messages 语言 遍历
    const locales = Object.keys(messages)
    for (const locale of locales) {
        //如果messages 包里面有系统语言返回
        if (language.indexOf(locale) > -1) {
            return locale
        }
    }
    // 默认语言 简体中文
    return 'zh-CN'
}

const i18n = createI18n({
    legacy: false,
    allowComposition: true,
    globalInjection: true,
    locale: getLocale(),
    messages,
})

export default i18n;